#include "arbre.hpp"
#include <sstream>

double Arbre::getPositionX()
{
	return coord[0];
}

double Arbre::getPositionY()
{
	return coord[1];
}

int Arbre::getAge()
{
	return age;
}

Type Arbre::getType()
{
	return type;
}

double Type::getRayonSecu()
{
	return rayonSecu;
}

int Type::getAgeMax()
{
	return age_max;
}
QVector<int> Type::getTabTypeSuperieur()
{
	return tabTypeSuperieur;
}
QVector<int> Type::getTabTypeInferieur()
{
	return tabTypeInferieur;
}
int Type::getReproduction()
{
	return reproduction;
}
int Type::getId()
{
	return id;
}
Vector Arbre::getCoord()
{
	return coord;
}
double Type::getDistanceReproduction()
{
	return distanceReproduction;
}
int Type::getNbGraine()
{
	return nbGraine;
}

MayaGeometryAll Type::rendu(MayaFrame f)
{
	MayaGeometryAll mga;
	MayaGeometrySet mgs;
	stringstream s;
	s<<"Feuille_"<<id;
	MaterialObject mfeuille={ ShaderPhong, None, AColor(0.0,1.0,0.0,1.0), AColor(0.0,1.0,0.0,1.0), AColor(0.1,0.1,0.1,1.0), 50.,QString("")};
	MaterialObject mTronc={ ShaderPhong, None, AColor(0.5,0.4,0.2,1.0), AColor(0.5,0.4,0.2,1.0), AColor(0.1,0.1,0.1,1.0), 50.,QString("")};
	MayaGeometry tronc("tronc",mTronc);
	tronc.Merge(tronc.CreateCylinder(Vector(0,0,-1),Vector(0,0,3),0.5,10));
	MayaGeometry feuille(s.str().c_str(),mfeuille);
	if(id==1)
	{
		feuille.Merge(feuille.CreateSphere(Vector(0,0,2.0),1.5,10));
	}
	else if(id==0)
	{
		feuille.Merge(feuille.CreateCone(Vector(0,0,2.5),Vector(0,0,6.5),2.0,10));
	}
	else if(id==2)
	{
		feuille.Merge(feuille.CreateIcosahedron(Vector(0,0,2.5),1.0));
	}
	mgs=MayaGeometrySet(tronc,f);
	mga.Append(mgs);
	mgs=MayaGeometrySet(feuille,f);
	mga.Append(mgs);
	return mga;
}

MayaFrame Arbre::renduFrame(Terrain *ter)
{
	MayaFrame res(ter->vec(getPositionX(),getPositionY()),Vector(1,0,0),Vector(0,1,0),Vector(0,0,1));
	if(age<20)
	{
		res.SetScale(Vector(1.0,1.0,1.0)*0.3);
	}
	else if(age<type.getAgeMax()*0.8)
	{
		res.SetScale(Vector(1.0,1.0,1.0)*0.7);
	}
	return res;
}

void Arbre::evolutionArbre(int nbTemps)
{
	age += nbTemps;
}